//
//  DTMemebersView.m
//  Objective-cDemo
//
//  Created by ShareAnimation on 2019/8/6.
//  Copyright © 2019 爱丽丝的梦境. All rights reserved.
//

#import "DTMemebersView.h"
#import "Masonry.h"
#import "DTMemeberCell.h"
@interface DTMemebersView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) UICollectionViewFlowLayout *flowLayout;
@property (strong, nonatomic) UICollectionView *collectionView;

@property (strong, nonatomic) NSArray *memebers;
@end

@implementation DTMemebersView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupViews];
        
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        

    }
    return self;
}

- (void)doSomething:(void(^)(CGFloat height))block {

    NSMutableArray *tempArr = [NSMutableArray new];
    for (NSInteger i=0; i<10; i++) {
        DTMemberCellModel *model = [DTMemberCellModel new];
        model.headUrl = @"222222";
        model.name = [NSString stringWithFormat:@"------%ld",i];
        model.cellType = DTMemberCellTypeNormal;
        
        [tempArr addObject:model];
    }
    
    DTMemberCellModel *model = [DTMemberCellModel new];
    model.cellType = DTMemberCellTypeAdd;
    [tempArr addObject:model];
    
    self.memebers = tempArr.copy;
    
    [self.collectionView reloadData];
    
    [self layoutIfNeeded];

    CGFloat height = self.collectionView.contentSize.height;
//    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self);
//        make.height.equalTo(@(height)).with.priority(700);
//    }];
    block(height);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    
}

- (void)setupViews {

}

#pragma mark - Delegate/DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.memebers.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor yellowColor];
//    return cell;
    
    DTMemeberCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DTMemeberCell" forIndexPath:indexPath];
    
    [cell bindModel:self.memebers[indexPath.row]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(60,80);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@",self.memebers[indexPath.row]);
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor redColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[DTMemeberCell class] forCellWithReuseIdentifier:@"DTMemeberCell"];
        
        
    }
    return _collectionView;
}
@end
